package com.example.springboot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Model;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Guide;
import com.example.springboot.mapper.GuideMapper;
import com.example.springboot.service.IGuideService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.service.INoteService;
import com.example.springboot.service.ISolutionService;
import com.example.springboot.utils.DirectionUtil;
import com.example.springboot.utils.KeywordMatchUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GuideServiceImpl extends ServiceImpl<GuideMapper, Guide> implements IGuideService {

    @Resource
    private IGuideService guideService;

    @Resource
    private INoteService noteService;

    @Resource
    private ISolutionService solutionService;

    //新增引导信息
    @Override
    public Result insertGuide(Guide guide) {
        QueryWrapper<Guide> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",guide.getUserId());
        queryWrapper1.eq("name",guide.getName());
        Guide one = guideService.getOne(queryWrapper1);
        //根据用户id和引导名查找，如果找到，插入失败
        if (one != null) {
            return Result.error(Constants.CODE_600,"您已创建过相同名称的创新引导，请前往个人中心查看。");
        }
        //若未在数据库中找到重复数据，将数据插入数据库，返回引导信息
        else {
            guideService.save(guide);
            one = guideService.getOne(queryWrapper1);
            return Result.success("创新引导创建成功，已为您生成专属书签。",one);
        }
    }

    //根据用户id 模糊搜索引导名
    @Override
    public List<Guide> findByName(Integer userId, String guideName) {
        LambdaQueryWrapper<Guide> wrapper= Wrappers.lambdaQuery();
        wrapper.eq(Guide::getUserId,userId);
        if (StrUtil.isNotBlank(guideName)){
            wrapper.like(Guide::getName,guideName);
        }
        return guideService.list(wrapper);
    }

    @Override
    public Result getModelLinks(Integer id) throws JsonProcessingException {
        DirectionUtil directionUtil = new DirectionUtil();
        Guide guide = guideService.getById(id);
        return Result.success(directionUtil.linksToString(guide.getModel()));
    }

    @Override
    public Result findByUser(Integer userId) {
        QueryWrapper<Guide> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Guide> result = guideService.list(queryWrapper);
        result.sort((t1,t2)->t2.getCreateTime().compareTo(t1.getCreateTime()));//按创建时间排序
        return Result.success(result);
    }

    @Override
    public Result createDirection(String jsonString) throws JsonProcessingException {
        DirectionUtil directionUtil = new DirectionUtil();
        Model model = directionUtil.transform(jsonString);
        return Result.success(directionUtil.createList(model));
    }

    @Override
    public Result recommendKeyWord(List<Integer> num) {
        List<String> keyWords = new ArrayList<>();
        KeywordMatchUtil kwm = new KeywordMatchUtil();
        for (int i : num) {
            keyWords.addAll(kwm.kwMatch(i));
        }
        return Result.success(keyWords);
    }

    @Override
    public boolean removeGuideInfo(Integer id) {
        boolean result = guideService.removeById(id);
        noteService.deleteByGuide(id);
        solutionService.removeSolutionByGuide(id);
        return result;
    }
}
